<!--  -->
<template>
  <!-- 添加新用户 -->
  <div class="add">
    <el-dialog
      v-model="addFlag"
      title="修改信息"
      :before-close="cancelAdd"
      @open="unitUserFrom"
    >
      <el-form :model="addUserFrom" label-width="120px">
        <el-form-item label="账号：">
          <el-input v-model="addUserFrom.username" />
        </el-form-item>
        <el-form-item label="姓名：">
          <el-input v-model="addUserFrom.nickName" />
        </el-form-item>
        <el-form-item label="邮箱：">
          <el-input v-model="addUserFrom.email" />
        </el-form-item>
        <el-form-item label="密码：">
          <el-input v-model="addUserFrom.password" type="password" />
        </el-form-item>
        <el-form-item label="备注：">
          <el-input v-model="addUserFrom.note" type="textarea" />
        </el-form-item>
        <el-form-item label="是否启用：">
          <el-radio-group v-model="addUserFrom.status">
            <el-radio :label="0"> 是</el-radio>
            <el-radio :label="1">否 </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="cancelAdd" size="large">取消</el-button>
        <el-button type="primary" @click="openAdd" size="large">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { ref, withDefaults } from "vue";
import * as http from "@/request";
let addUserFrom = ref({} as IAddUserInfo);
// const props = defineProps({
//   addFlag: Boolean,
// });
const props = withDefaults(defineProps<{ addFlag: boolean }>(), {
  addFlag: false,
});

const emit = defineEmits<{
  (event: "openAddDialog"): void;
  (event: "closeAddDialog"): void;
}>();
const cancelAdd = () => {
  emit("closeAddDialog");
};
const unitUserFrom = () => {
  addUserFrom.value = {} as IAddUserInfo;
};
const openAdd = () => {
  ElMessageBox.confirm("此操作将添加一个新的用户, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true,
  })
    .then(async () => {
      let res = await http.adminRegister(addUserFrom.value);
      if (res.code === 200) {
        emit("closeAddDialog");
        // 重新获取当前用户信息
        // unitPage();
        ElMessage({
          message: res.message,
          type: "success",
        });
      } else {
        ElMessage({
          message: res.message,
          type: "error",
        });
      }
    })
    .catch(() => {});
};
</script>
<style lang="scss" scoped></style>
